import { LoginUser, BaseInfo } from '../interfaces'
import _ from 'lodash'

export const UUID = (prefix = '') => prefix + Date.now().toString(36) + '-' + Math.floor(Math.random() * Number.MAX_SAFE_INTEGER).toString(36)
export const formatString = (str = '', len = 16) => (str + '*'.repeat(len)).substring(0, len)

/** 部分服务端错误定义 */
export const ERRORS = {
    CHATROOM_404: '群聊不存在或已删除',
    MESSAGE_404: '消息不存在',
    NOT_LOGIN: '未登录',
    NO_USERS: '获取用户列表失败',
    PERMISSION_DENIED: '权限不足',
    MUTED_ALL: '当前群禁言中',
    MUTED_YOU: '你已被禁言',
    DEL_YOU: '你已移除群聊',
    PARAMS: '参数错误',
    OUTOFSIZE: '请求数据过大',
    UNKNOWN: '服务端异常'
} as const


export const logger = {
    log: (type: 'Info' | 'Error' | 'Timeout' = 'Info', ...args: any[]) => {
        console.log.apply(console, [new Date().toLocaleString(), `${type}\t`, ...args])
    }
}

export const TemplateRender = (tmpl?: string, obj: any = {}, placeholder = '') => tmpl?.
  replace(/\$\{([\.\w]+)\}/g, function ($, key) {
    return _.get(obj, key) || placeholder
  })

export const getBaseInfo = (loginUser: LoginUser, info?: BaseInfo | null) => {
    return {
        created_by: info?.created_by || loginUser?.username,
        gmt_created: info?.gmt_created || Date.now(),
        modified_by: loginUser?.username || info?.modified_by,
        gmt_modified: Date.now() || info?.gmt_modified,
    } as Omit<BaseInfo, '_id'>
}

